home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 21 / Cream of the Crop 21 (Terry Blount) (October 1996).iso / program / freeli22.zip / PROGS / ZE.ASM < prev    next >
Assembly Source File  |  1996-09-01  |  5KB  |  165 lines

  1. ; ZE: Like UUencode, but 27% instead of 40% expansion.
  2.  
  3. Ideal
  4. Jumps
  5.  
  6. Public      main
  7. Extrn       startup:near
  8.  
  9. Macro       lcall p,a,b,c,d,e,f,g,h ;; library call
  10.  
  11.             ifnb <a>
  12.               push a                ;; if args, push first arg
  13.               lcall p,b,c,d,e,f,g,h ;; and recurse . . .
  14.             else
  15.               extrn p:near          ;; declare procedure
  16.               call p                ;; call procedure
  17.             endif
  18.  
  19. EndM
  20.  
  21. Model Tiny
  22. Codeseg
  23. P186
  24. Org 100h
  25.  
  26. Start:      jmp startup
  27.  
  28. ;****************** Data Section
  29.  
  30. FSize       dw 0,0                  ;File size
  31.  
  32. ;****************** Strings Section
  33.  
  34. Syntax      db 'Syntax:  ZE <infile> <outfile>',0
  35.  
  36. ;****************** 'main' procedure
  37.  
  38. Proc        main
  39.  
  40.             lcall fsetbuf 16384     ;Set file buffers to 16K
  41.  
  42.             cmp cx,2                ;Wrong number of args?
  43.             jne m_syntax
  44.  
  45.             lcall fopen [di],0      ;Open input file
  46.             test ax,ax              ;File not found?
  47.             jz m_syntax
  48.             xchg bp,ax              ;BP = handle
  49.  
  50.             lcall fopen [di+2],3    ;Open output file
  51.             test ax,ax              ;Check for errors
  52.             jnz m_ok1
  53.             lcall fclose bp         ;Close input file
  54.             jmp m_syntax            ;Go print syntax
  55.  
  56. m_ok1:      xchg di,ax              ;DI = handle
  57.  
  58.             lcall fseek bp,0,0,2    ;Get file size
  59.             lcall ftell bp
  60.             mov [FSize],ax          ;Save size
  61.             mov [FSize+2],dx
  62.             lcall fseek bp,0,0,0    ;Seek to beginning
  63.  
  64.             mov cx,[FSize+2]        ;CX:BX = size
  65.             mov bx,[FSize]
  66.             mov si,8                ;8 digits
  67.  
  68. m_sloop:    mov dx,4                ;4 bits
  69.  
  70. m_01:       add bx,bx               ;Shift in bit
  71.             adc cx,cx
  72.             adc al,al
  73.             dec dx                  ;Loop back
  74.             jnz m_01
  75.  
  76.             and al,0Fh              ;Convert to hex
  77.             cmp al,0Ah
  78.             sbb al,69h
  79.             das
  80.             lcall fputc di,ax       ;Output char
  81.             dec si
  82.             jnz m_sloop             ;Loop back
  83.  
  84.             lcall fgetc bp          ;Preload bit buffer
  85.             mov [BitBuf],al
  86.  
  87.             mov cx,4                ;4 digrams output
  88.             mov dl,91               ;DL = divisor
  89.  
  90. m_loop:     push bp                 ;Get 13-bit code
  91.             call Get13Bits
  92.             div dl                  ;Convert to Mod91
  93.             add ax,2121h
  94.  
  95.             mov bl,ah               ;Output digram
  96.             lcall fputc di,ax
  97.             xchg bx,ax
  98.             lcall fputc di,ax
  99.  
  100.             inc cx                  ;Increment counter
  101.             cmp cx,35               ;End of line?
  102.             jb m_skip
  103.  
  104.             xor cx,cx               ;Reset counter
  105.             lcall fputc di,13       ;Output CR, LF
  106.             lcall fputc di,10
  107.  
  108. m_skip:     cmp [FSize+2],0         ;Check size
  109.             jnl m_loop              ;Loop back
  110.  
  111.             lcall fputc di,7Eh      ;Output tilde
  112.             lcall fputc di,13       ;Output CR, LF
  113.             lcall fputc di,10
  114.  
  115. m_done:     lcall fclose bp         ;Close files
  116.             lcall fclose di
  117.             ret                     ;Return
  118.  
  119. m_syntax:   push offset(Syntax)     ;Display 'Syntax' message
  120.             lcall puts
  121.             ret                     ;Return
  122.  
  123. EndP        main
  124.  
  125. ;****************** Get13Bits -- Get 13 bits from input file
  126. ;int Get13Bits(FILE *input);
  127.  
  128. Proc        Get13Bits
  129.  
  130.             push bp                 ;Set up stack frame
  131.             mov bp,sp
  132.             push bx cx dx           ;Save registers
  133.  
  134.             mov dx,[bp+4]           ;DX = file
  135.             mov cx,13               ;13 bits
  136.             xor bx,bx               ;Zero buffer
  137.  
  138. gb_loop:    add bx,bx               ;Shift in bit
  139.             mov al,[BitMask]        ;Test bit
  140.             test al,[BitBuf]
  141.             jz $+3
  142.             inc bx                  ;Set bit
  143.  
  144.             ror [BitMask],1         ;Rotate mask
  145.             jnc gb_skip             ;Check for wrap
  146.             lcall fgetc dx          ;Get byte
  147.             mov [BitBuf],al
  148.             sub [FSize],1           ;Decrement size
  149.             sbb [FSize+2],0
  150.  
  151. gb_skip:    loop gb_loop            ;Loop back
  152.  
  153.             xchg bx,ax              ;Result in AX
  154.  
  155. gb_done:    pop dx cx bx            ;Restore registers
  156.             pop bp                  ;Delete stack frame
  157.             ret 2                   ;Return
  158.  
  159. BitMask     db 80h                  ;Mask and buffer
  160. BitBuf      db 0
  161.  
  162. EndP        Get13Bits
  163.  
  164. End Start
  165.